package com.chinasoft.mall.framework;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
  
public class HttpJsonAdapter {  
  
    private static HttpJsonAdapter instance = null;  
  
    private HttpJsonAdapter() {  
    }  
  
    public static synchronized HttpJsonAdapter getInstance() {  
        if (instance == null) {  
            instance = new HttpJsonAdapter();  
        }  
        return instance;  
    }  
  
    // json转为带泛型的list  
    public <T> List<T> gets(String str, Class<T> clazz) {  
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")  
                     .create();  
             // json转为带泛型的list  
             return gson.fromJson(str, new TypeToken<List<T>>() {  
             }.getType());  
        }  
  
    public <T> T get(String str, Class<T> clazz) throws Exception {  
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")  
                .create();  
        try {  
            return (T) gson.fromJson(str, clazz);  
        } catch (Exception e) {  
            throw new Exception("数据处理异常", e);  
        }  
    }  
  
    public static String getMapToJson(Object obj) {  
        Gson gson = new Gson();  
        HashMap<String, Object> objmap = new HashMap<String, Object>();  
        return gson.toJson(toGson(objmap, obj, obj.getClass()));  
    }  
  
    /** 只支持继承 * */  
    public static HashMap<String, Object> toGson(  
            HashMap<String, Object> objmap, Object obj, Class<?> clazz) {  
        // 父类  
        Class<?> SuperClass = clazz.getSuperclass();  
        // 接口数组  
        // Class[] InterClass=clazz.getInterfaces();  
        Field[] ffields = clazz.getSuperclass().getDeclaredFields();  
        Field[] fields = clazz.getDeclaredFields();  
        for (Field field : fields) {  
            boolean flag = field.isAccessible();  
            field.setAccessible(true);  
            try {  
                objmap.put(field.getName(), field.get(obj));  
            } catch (IllegalArgumentException e) {  
            } catch (IllegalAccessException e) {  
                e.printStackTrace();  
            }  
            field.setAccessible(flag);  
        }  
        for (Field field : ffields) {  
            boolean flag = field.isAccessible();  
            field.setAccessible(true);  
            try {  
                objmap.put(field.getName(), field.get(obj));  
            } catch (IllegalArgumentException e) {  
  
            } catch (IllegalAccessException e) {  
                e.printStackTrace();  
            }  
            field.setAccessible(flag);  
        }  
        if (SuperClass != Object.class) {  
            return toGson(objmap, obj, SuperClass);  
        } else {  
            return objmap;  
        }  
    }  
  
}  